package com.nbc.car.biz.web;

import com.nbc.car.biz.dto.UserBizDTO;
import com.nbc.car.biz.entity.CUser;
import com.nbc.car.biz.enums.UserBizEnum;
import com.nbc.car.biz.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by wenbinnie on 16/7/21.
 */
@Controller
@RequestMapping("/user")
public class UserController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private IUserService iUserService;

    @RequestMapping(value = "/register",method = RequestMethod.POST,produces = {"application/json;charset=utf-8"})
    @ResponseBody//看到该注解时，spring会把返回类型封装成json
    public UserBizDTO<String> register (CUser cUser){
        try {
        boolean register = iUserService.register(cUser);
            if (register){
                return new UserBizDTO<String>(UserBizEnum.SUCCESS);
            }else{
                return new UserBizDTO<String>(UserBizEnum.REGISTER_FAIL);
            }
        } catch (Exception e){
            return new UserBizDTO<String>(e.getMessage(),UserBizEnum.SYSTEM_ERROR);
        }
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST,produces = {"application/json;charset=utf-8"})
    @ResponseBody//看到该注解时，spring会把返回类型封装成json
    public UserBizDTO<CUser> login (CUser cUser){
        CUser cuser = iUserService.login(cUser);
        if (cuser != null){
            return new UserBizDTO<CUser>(cuser, UserBizEnum.SUCCESS);
        }else{
            return new UserBizDTO<CUser>(UserBizEnum.LOGIN_FAIL);
        }

    }


}
